package crm.view.statistic.treetable;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.tree.TreePath;

import crm.model.teacherPC.CRMListener;
import crm.model.teacherPC.Manager;
import crm.model.teacherPC.net.StudentPC;
import crm.model.teacherPC.statistic.Statistic;
import crm.model.teacherPC.statistic.StatisticValue;
import crm.util.treetable.AbstractTreeTableModel;
import crm.util.treetable.TreeTableModel;

public class StatisticTreeTableModel extends AbstractTreeTableModel implements TreeTableModel, CRMListener{


	static protected String[] cNames = { "Programmname", "Zeit", "%" };
	static protected Class[] cTypes = { TreeTableModel.class, String.class, String.class, String.class };

	Manager manager;
	public StatisticTreeTableModel(Manager manager) {
		super(new StartNode(manager));
		manager.addListener(this);
		this.manager=manager;

		// TODO Auto-generated constructor stub
	}
	@Override
	public boolean isLeaf(Object node) {
		// TODO Auto-generated method stub
		return getChildCount(node) == 0 || node instanceof StatisticValue || node instanceof String;
	}


	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return cNames.length;
	}
	@Override
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return cNames[column];
	}

	@Override
	public Object getValueAt(Object node, int column) {
		System.out.println(node + ":" + column);
		if (node instanceof StartNode){
			Manager manager = ((StartNode)node).manager;
			switch (column){
			case 0:
				return "Statistic";
			}
			return "";
		}else if (node instanceof StudentPC){
			StudentPC student = ((StudentPC)node);

			switch (column){
			case 0:
				return "" + student.getUser();
			}
			return "";

		}else if (node instanceof StatisticValue){
			StatisticValue sValue = (StatisticValue)node;

			switch (column){
			case 0:
				return "" + sValue.getName();
			case 1:
				long ms = sValue.getTime();
				Date date = new Date(ms);
				SimpleDateFormat d = new SimpleDateFormat("mm:ss");
				return "" + d.format(date);
			case 2:
				return "" + ((int)((sValue.getTime() / ((double)sValue.getStatistic().sumTime) ) * 100)) + "%";
			}
		}

		return null;
	}

	@Override
	public Object getChild(Object parent, int index) {
		if (parent instanceof StartNode){
			Manager manager = ((StartNode)parent).manager;
			System.out.println(manager.getStudent(index));
			return manager.getStudent(index);
		}else if (parent instanceof StudentPC){
			StudentPC student = (StudentPC)parent;
			Statistic stat = student.statistic;
			System.out.println(stat.map.values().toArray()[index]);
			return stat.map.values().toArray()[index];
		}else {
			return "";
		}
		
	}

	@Override
	public int getChildCount(Object parent) {
		if (parent instanceof StartNode){
			Manager manager = ((StartNode)parent).manager;
			return manager.getStudentCount();
		}else if (parent instanceof StudentPC){
			Statistic stat = ((StudentPC)parent).statistic;
			return stat.map.size();
		}
		return 0;
	}

	@Override
	public Class getColumnClass(int column) {
		// TODO Auto-generated method stub
		return cTypes[column];
	}
	@Override
	public void actualisingComplete(StudentPC student) {
		fireTreeStructureChanged(this, new TreePath(new Object[]{getRoot(), student}));

	}
	@Override
	public void connectionClosed() {
		// TODO Auto-generated method stub

	}
	@Override
	public void connectionStarted() {
		// TODO Auto-generated method stub

	}
	@Override
	public void exitCRM() {
		// TODO Auto-generated method stub

	}
	@Override
	public void studentAdded(StudentPC student) {
		System.out.println("insertnode");

		fireTreeNodesInserted(this, new Object[]{getRoot()}, new int[]{1}, new Object[]{student});
	}
	@Override
	public void studentRemoved(StudentPC student) {
		// TODO Auto-generated method stub

	}


}
class StartNode {
	Manager manager;
	public StartNode(Manager manager) {
		this.manager=manager;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "Statistic";
	}
}
